package com.xcc.shopanim.newa;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xcc.shopanim.BaseActivity;
import com.xcc.shopanim.BaseFragment;
import com.xcc.shopanim.OtherFragment;
import com.xcc.shopanim.R;
import com.xcc.shopanim.utils.NotchUtils;
import com.xcc.shopanim.utils.QMUIStatusBarHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements BaseFragment.OnFragmentCallBack {
    @BindView(R.id.tab3)
    ImageView tab3;
    @BindView(R.id.imgRedOpen)
    ImageView imgRedOpen;
    @BindView(R.id.layoutBottom)
    View layoutBottom;
    private int notchHeight;

    public static void open(Activity context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBar();
        setContentView(R.layout.newa_activity_main);
        ButterKnife.bind(this);

        fragments = new BaseFragment[]{
                Main2Fragment.newInstance()
                , OtherFragment.newInstance()};

        removeFragment(savedInstanceState);
    }

    protected void asynRun() {
        QMUIStatusBarHelper.setStatusBarDarkMode(getActivity());//白色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            notchHeight = NotchUtils.getNotchHeight(getActivity());
        }
        onViewClicked(tab3);
        /*
         *傻逼Y97卸载安装，第一次启动不显示layoutFrame，
         * 这样设置一下就可以了，(@﹏@)~
         */
        View view = findViewById(R.id.layoutFrame);
        view.setBackgroundColor(Color.TRANSPARENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(0xFF1B1B1B);
        }
    }

    private View oldView;

    @OnClick({R.id.tab3, R.id.tab5})
    public void onViewClicked(View view) {
        if (view == null) return;
        if (view == oldView) return;
        if (oldView != null) oldView.setSelected(false);
        oldView = view;
        oldView.setSelected(true);
        switch (view.getId()) {
            case R.id.tab3:
                opertBottom(0);
                gotoFragment(fragments[0]);
                break;
            case R.id.tab5:
                opertBottom(1);
                gotoFragment(fragments[1]);
                break;
        }
    }

    @OnClick(R.id.imgRedOpen)
    public void onImgRedOpenClicked() {
        if (oldFragment instanceof Main2Fragment) {
            oldFragment.sendMessage(0xF0011, null);
        }
    }

    /**
     * 操作底部控件
     *
     * @param type 0显示红按钮，1不显示
     */
    private void opertBottom(int type) {
        if (opertBottom == type) return;
        if (type == 0 && fragmentType != 0) return;
        opertBottom = type;
        if (type == 0) {
            imgRedOpen.setVisibility(View.VISIBLE);
            layoutBottom.setBackgroundResource(R.drawable.img_main_bg);
        } else {
            imgRedOpen.setVisibility(View.GONE);
            layoutBottom.setBackgroundColor(0xf01D1D1D);//29 29 29, 0.94
        }
    }

    private void changeRedIcon(boolean open) {
        imgRedOpen.setImageResource(open ? R.drawable.ic_img_main_reddot_ : R.drawable.ic_img_main_reddot);
    }

    public void setFragmentType(int fragmentType) {
        this.fragmentType = fragmentType;
        opertBottom(fragmentType);
    }

    private int fragmentType;//0发现 1其他
    private int opertBottom;
    private BaseFragment[] fragments;
    public BaseFragment oldFragment;

    private void gotoFragment(BaseFragment fragment) {
        if (fragment == null) return;
        if (fragment == oldFragment) return;
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.layoutFrame, fragment);
        }
        if (oldFragment != null) transaction.hide(oldFragment);
        this.oldFragment = fragment;
        transaction.commitAllowingStateLoss();
    }

    public Object onFragmentCallBack(BaseFragment fragment, int type, Object object) {
        Object obj = null;
        switch (type) {
            case 3://获取状态栏高度
                obj = notchHeight;
                break;
            case 7://更改icon
                if (object instanceof Boolean) changeRedIcon((Boolean) object);
                break;
            case 8://更改是否显示
                if (object instanceof Boolean) {
                    Boolean bb = (Boolean) object;
                    setFragmentType(bb ? 0 : 1);
                }
                break;
        }
        return obj;
    }

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (oldFragment == null) {
            super.onActivityResult(requestCode, resultCode, data);
        } else oldFragment.onActivityResult(requestCode, resultCode, data);
    }
}
